﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Anno
{
    /// <summary>
    /// 参数助手
    /// </summary>
    public static class ArgsValue
    {
        /// <summary>
        /// 根据名称 获取参数字符串
        /// </summary>
        /// <param name="name">名称 例如 【-p】</param>
        /// <param name="args">字符串数组</param>
        /// <returns></returns>
        public static string GetValueByName(string name,string[] args)
        {
            for (var i = 0; i < args.Length; i++)
            {
                if (string.Equals(args[i].Trim(), name.Trim(), StringComparison.CurrentCultureIgnoreCase)&& args.Length>=(i+1))
                {
                    return args[i + 1];
                }
            }
            return null;
        }

        ///// <summary>
        ///// 根据键获取值 string
        ///// </summary>
        ///// <param name="key"></param>
        ///// <returns>string Value</returns>
        //public static string RequestString(this IDictionary<string, string> _input, string key)
        //{
        //    if (GetInputValueByKey(_input, key, out var val) && !string.IsNullOrWhiteSpace(val))
        //    {
        //        return PathUtil.UrlDecode(val);
        //    }
        //    else
        //    {
        //        return "";
        //    }
        //}
        /// <summary>
        /// 根据键获取值 string
        /// </summary>
        /// <param name="key"></param>
        /// <returns>string Value</returns>
        public static string RequestString(this IDictionary<string, string> _input, string key, string defv = "")
        {
            if (GetInputValueByKey(_input, key, out var val) && !string.IsNullOrWhiteSpace(val))
            {
                return PathUtil.UrlDecode(val);
            }
            else
            {
                return defv;
            }
        }
        /// <summary>
        /// 根据Key 获取字符串值
        /// </summary>
        /// <param name="key">键</param>
        /// <returns>字符串值</returns>
        public static bool GetInputValueByKey(this IDictionary<string, string> _input,string key, out string val,string bodykey = "body")
        {
            val = null;
            if (_input == null || _input.Count == 0) return false;
            if (_input.ContainsKey(key))
            {
                val = _input[key];
                return true;
            }
            else
            {
                bodykey = _input.Keys.FirstOrDefault(f => f.Equals(bodykey, StringComparison.OrdinalIgnoreCase));
                if (!string.IsNullOrWhiteSpace(bodykey))
                {
                    try
                    {
                        var json = (Newtonsoft.Json.Linq.JObject)JsonHelper.DeserializeObjectByJson(_input[bodykey]);
                        if (json != null)
                        {
                            Newtonsoft.Json.Linq.JToken valueStr;
                            if (json.TryGetValue(key, StringComparison.OrdinalIgnoreCase, out valueStr))
                            {
                                if (valueStr != null)
                                {
                                    val = valueStr.ToObject<string>();
                                    return true;
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("body 转JSON对象时异常(GetInputValueByKey)：body非正确JSON格式，" + ex.Message);
                    }
                }
                return false;
            }
        }
    }
}
